package com.ayh.campusfun.controller.api.v1.oss;

import com.ayh.campusfun.common.CommonResult;
import com.ayh.campusfun.common.OSSResult;
import com.ayh.campusfun.common.core.oss.OSSService;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ayh
 * @create 2021/4/2 0002 15:20
 * oss控制器
 */


@RestController
@RequestMapping("oss-file")
public class OSSController {

    @Resource
    OSSService ossService;

    @PostMapping(value = "/uploadFile",produces = "application/json")
    public CommonResult uploadFile(MultipartFile file) throws Exception {
        OSSResult ossResult = ossService.uploadFile(file, "file");
//        Long time = ossService.getTime(file);
//        ossService.randomGrabberFFmpegVideoImage("https://campus-fun.oss-cn-beijing.aliyuncs.com/img/0ced3434-c84e-44dc-a2af-268d1185d960");
//        Long time = ossService.getTime("https://campus-fun.oss-cn-beijing.aliyuncs.com/img/0ced3434-c84e-44dc-a2af-268d1185d960");
        return CommonResult.success(ossResult);
    }

    @PostMapping(value = "/uploadVideo",produces = "application/json")
    public CommonResult uploadVideo(MultipartFile file) throws Exception {
        OSSResult ossResult = ossService.uploadFile(file, "video");
        Long time = ossService.getTime(file);
        OSSResult videoImageResult = ossService.randomGrabberFFmpegVideoImage(ossResult.getFileUrl());
        ossResult.setTime(time.toString());
        ossResult.setCover(videoImageResult.getFileUrl());
        return CommonResult.success(ossResult);
    }

    @PostMapping(value = "/uploadFiles",produces = "application/json")
    public CommonResult uploadFiles(
            MultipartRequest request
            ) throws Exception {
        MultiValueMap<String, MultipartFile> multiFileMap = request.getMultiFileMap();
        List<OSSResult> ossResultList = ossService.uploadFiles(multiFileMap, "img");
//        Long time = ossService.getTime("\\https://campus-fun.oss-cn-beijing.aliyuncs.com/img/0ced3434-c84e-44dc-a2af-268d1185d960");
        return CommonResult.success(ossResultList);
    }

}
